﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_TempLivingExpense : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        ICostLivingsBLL costLivengsBLL = new CostLivingsBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            txtSalaryMax2.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtSalaryMin2.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtAdd2.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            if (!IsPostBack)
            {
                LoadData();
            }
            
        }

        private void LoadData()
        {
            try
            {
                DataSet ds = new DataSet();
                ds = costLivengsBLL.ListCostLivingsFilter();

                if (ds.Tables[0].Rows.Count > 0)
                {
                    lblID.Text = ds.Tables[0].Rows[0]["cost_seq"].ToString();
                    txtSalaryMax2.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["cost_max"].ToString());
                    lblSalaryMax.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["cost_max"].ToString());

                    txtSalaryMin2.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["cost_min"].ToString());
                    lblSalaryMin.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["cost_min"].ToString());
                    txtAdd2.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["cost_add"].ToString());
                      
                } 
                RadGrid2.DataSource = costLivengsBLL.ListCostLivingsAll();
                RadGrid2.DataBind();

                modeView();

            }
            catch (Exception ex)
            {
                Web.ShowAlert(this, ex.Message);
            }
        }

        private void modeUpdate()
        {
            btnEdit.Visible = false;
            btnInsert.Visible = false;
         //   btnSave.Visible = true;
            btnCancel.Visible = true;
            txtSalaryMax2.ReadOnly = false;
            txtSalaryMin2.ReadOnly = false;
            txtAdd2.ReadOnly = false;

            txtSalaryMax2.BackColor = System.Drawing.Color.White;
            txtSalaryMin2.BackColor = System.Drawing.Color.White;
            txtAdd2.BackColor = System.Drawing.Color.White;
        }
        private void modeView()
        {
            btnEdit.Visible = true;
            btnInsert.Visible = true;
            btnSave.Visible = false;
            btnSaveInsert.Visible = false;
            btnCancel.Visible = false;
            PanelAdd.Visible = false;

            txtSalaryMax2.BackColor = System.Drawing.ColorTranslator.FromHtml("#A2D3EB");
            txtSalaryMin2.BackColor = System.Drawing.ColorTranslator.FromHtml("#A2D3EB");
            txtAdd2.BackColor = System.Drawing.ColorTranslator.FromHtml("#A2D3EB");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            modeUpdate();
            btnSave.Visible = true;
            PanelAdd.Visible = false;
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            modeUpdate();
            PanelAdd.Visible = true;
            txtSalaryMax2.Text = "";
            txtSalaryMin2.Text = "";
            txtAdd2.Text = "";
            btnSaveInsert.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            LoadData();
            modeView();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        { 
                costLivengsBLL.Update(int.Parse(lblID.Text), Convert.ToDouble(txtSalaryMin2.Text), Convert.ToDouble(txtSalaryMax2.Text), Convert.ToDouble(txtAdd2.Text), "", SystemInfo._ActiveRecordStatus, ssMng.EmpSeq);
            LoadData();
            modeView();
        }


        protected void btnSaveInsert_Click(object sender, EventArgs e)
        {
            string mydate = txtDateStrat.GetStringValue();  //18/03/2553
            if (mydate == "")
            {
                //Utils.Web.ShowAlert(this.Page, "กรุณาระบุวันที่เริ่มมีผล!!");
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "AlertWindow('" + "กรุณาระบุวันที่มีผล" + "')", true);
                return;
            }
            else
            {
                costLivengsBLL.Insert(txtRefNo.Text, DateTime.Parse(mydate), null
                    , Convert.ToDouble(txtSalaryMin2.Text)
                    , Convert.ToDouble(txtSalaryMax2.Text)
                    , Convert.ToDouble(txtAdd2.Text), ""
                    , SystemInfo._ActiveRecordStatus
                    , ssMng.EmpSeq);
                LoadData();
                modeView();
            }
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cost_seq"]);
                if (seq == int.Parse(lblID.Text))
                {
                    e.Item.Cells[2].Text = "Active";
                    e.Item.Cells[2].ForeColor = System.Drawing.Color.Green;
                }
            }
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Delete")
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cost_seq"]);
                    costLivengsBLL.Delete(seq, ssMng.EmpSeq);
                    LoadData();
                }
                else if (e.CommandName == "InitInsert")
                {
                    PanelAdd.Visible = true;
                    modeUpdate();
                }
                else
                {
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                Web.ShowAlert(this, ex.Message);
            }
        } 

        protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("cmdAddNew") as LinkButton;
                button.Enabled = false;
                button.Visible = true;
                button.Text = "";
            }
        }
    }
}
